use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct Word {
    pub value: String,
    pub score: f64,
}

impl Word {
    pub fn new(value: String, score: f64) -> Self {
        Word { value, score }
    }
}

// 定义 AbstractDagParams trait
pub trait AbstractDagParams {
    fn get_phrase(&self, pinyin_list: &[String], num: usize) -> Vec<Word>;
}

pub struct DefaultDagParams {
    char_dict: HashMap<String, Vec<Word>>,
    phrase_dict: HashMap<String, Vec<Word>>,
}

impl DefaultDagParams {
    pub fn new(
        char_dict: HashMap<String, Vec<Word>>,
        phrase_dict: HashMap<String, Vec<Word>>,
    ) -> Self {
        Self {
            char_dict,
            phrase_dict,
        }
    }
}

impl AbstractDagParams for DefaultDagParams {
    fn get_phrase(&self, pinyin_list: &[String], num: usize) -> Vec<Word> {
        if pinyin_list.is_empty() {
            return vec![];
        }

        let data = if pinyin_list.len() == 1 {
            &self.char_dict
        } else {
            &self.phrase_dict
        };
        let pinyins = pinyin_list.join(",");

        if let Some(phrase_map) = data.get(&pinyins) {
            phrase_map[..std::cmp::min(num, phrase_map.len())].to_vec()
        } else {
            vec![]
        }
    }
}
